package com.leonzx.base.controller.userpermission;

import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.mail.UserMailService;
import com.leonzx.base.service.system.UserRoleService;
import com.leonzx.base.service.system.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/6/5 10:20
 */
@Controller
@RequestMapping("/userpermission/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserMailService userMailService;


    /**
     * 静态模版所在路径前缀
     */
    private String prefix = "userpermission/user";

    /**
     * 跳转到用户管理列表
     *
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("/userpermission/user/list")
    public String list() {
        return prefix + "/list";
    }

    /**
     * 跳转到用户添加页面
     *
     * @return
     */
    @RequestMapping("/add")
    @RequiresPermissions("/userpermission/user/list")
    public String add(User user, Model model) {
        model.addAttribute("user", user);
        return prefix + "/edit";
    }

    /**
     * 编辑跳转到详情页面
     *
     * @return
     */
    @RequestMapping("/edit")
    @RequiresPermissions("/userpermission/user/list")
    public String edit(User user, Model model) {
        User user1 = userService.getUserById(user.getId());
        model.addAttribute("user", user1);
        return prefix + "/edit";
    }


    /**
     * 详情还是跳转到详情页面
     *
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("/userpermission/user/list")
    public String detail(User user, Model model) {
        User user1 = userService.getUserById(user.getId());
        model.addAttribute("user", user1);
        model.addAttribute("btnstate", 1);
        return prefix + "/edit";
    }

    /**
     * 人员列表跳转到分配角色
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/userrole")
    @RequiresPermissions("/userpermission/user/list")
    public String userrole(User user, Model model) {
        model.addAttribute("user", user);
        model.addAttribute("isTemp", 0);//不是临时角色
        model.addAttribute("roleIds", userRoleService.findRoleStrByUserId(user.getId()));
        return "userpermission/role/choose";
    }
    /**
     * 人员列表跳转到分配临时角色
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/userroletemp")
    @RequiresPermissions("/userpermission/user/list")
    public String userroletemp(User user, Model model) {
        model.addAttribute("user", user);
        model.addAttribute("isTemp", 1);//是临时角色
        model.addAttribute("roleIds", userRoleService.findRoleStrByUserId(user.getId()));
        return "userpermission/role/choose";
    }

    /**
     * 跳转到临时分配权限
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/usermenutemp")
    @RequiresPermissions("/userpermission/user/list")
    public String userMenuTemp(User user, Model model) {
        model.addAttribute("user", user);
        return "userpermission/menu/menutemp-choose";
    }

    @RequestMapping("/userMailEdit")
    public String userMail(String userId, Model model) {
        model.addAttribute("userId", userId);
        return prefix + "/editMail";
    }

}
